package com.haofengsoft.scce.popupwin;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.*;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.R;

/**
 * Created by admin on 2015/7/24.
 */
public class CommentPopupWindow extends PopupWindow {
    private View backView;
    private View popView;
    private static final int ALPHA_DURATION = 300;
    private static final int TRANSLATE_DURATION = 200;
    private EditText feedbackInfoEt;
    private CommentPopWinInterface popWinInterface;
    private Context context;

    public CommentPopupWindow(Context context, CommentPopWinInterface popWinInterface) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.consultation_feedback, null);
        this.popWinInterface = popWinInterface;
        this.context = context;
        backView = (View) view.findViewById(R.id.consultation_feedback_rl);
        popView = (View) view.findViewById(R.id.consultation_feedback_pop_rl);
        setShowAnimation();
        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        view.setOnTouchListener(new TouchListener());

        // 输入法不遮挡popWindow
        this.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // editText 获取焦点
        feedbackInfoEt = (EditText) view.findViewById(R.id.consultation_feedback_info_et);

        feedbackInfoEt.setFocusable(true);
        feedbackInfoEt.setFocusableInTouchMode(true);
        feedbackInfoEt.requestFocus();
        // 打开输入法
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!inputManager.isActive(feedbackInfoEt)) {
            inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }

        TextView feedbackSubmitTv = (TextView) view.findViewById(R.id.consultation_feedback_submit_tv);
        feedbackSubmitTv.setOnClickListener(new FeedBackSubmitClickListener());
    }

    class FeedBackSubmitClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            String content = feedbackInfoEt.getText().toString();
            if (StringUtil.isNotBlank(content)) {
                popWinInterface.commentContent(content);
                dismiss();
            } else {
                Toast.makeText(context, "请输入发表的内容", Toast.LENGTH_SHORT).show();
            }

        }
    }

    private void setShowAnimation() {
        backView.startAnimation(createAlphaInAnimation());
        popView.startAnimation(createTranslationInAnimation());
    }

    private void setDismissAnimation() {
        backView.startAnimation(createAlphaOutAnimation());
        popView.startAnimation(createTranslationOutAnimation());
    }

    class TouchListener implements View.OnTouchListener {

        public boolean onTouch(View v, MotionEvent event) {

            int height = popView.getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        }

    }

    private Animation createAlphaInAnimation() {
        AlphaAnimation an = new AlphaAnimation(0, 1);
        an.setDuration(ALPHA_DURATION);
        return an;
    }

    private Animation createTranslationInAnimation() {
        int type = TranslateAnimation.RELATIVE_TO_SELF;
        TranslateAnimation an = new TranslateAnimation(type, 0, type, 0, type, 1, type, 0);
        an.setDuration(TRANSLATE_DURATION);
        return an;
    }

    private Animation createAlphaOutAnimation() {
        AlphaAnimation an = new AlphaAnimation(1, 0);
        an.setDuration(ALPHA_DURATION);
        an.setFillAfter(true);
        return an;
    }

    private Animation createTranslationOutAnimation() {
        int type = TranslateAnimation.RELATIVE_TO_SELF;
        TranslateAnimation an = new TranslateAnimation(type, 0, type, 0, type, 0, type, 1);
        an.setDuration(TRANSLATE_DURATION);
        an.setFillAfter(true);
        return an;
    }

    @Override
    public void dismiss() {
        setDismissAnimation();
        super.dismiss();
    }
}
